﻿using System;

using Ah.Testudin.Engine.Events;
using Ah.Testudin.Engine.Events.Components;
using Ah.Testudin.Engine.Graphics;
using Ah.Testudin.Engine.Graphics.Scenes;
using Ah.Testudin.Engine.Graphics.Scenes.SceneNodes;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ah.Testudin.Engine.Actors.Components.Render
{
    public class PrimitiveRenderComponent : IRenderComponent
    {
        #region Constructors

        public PrimitiveRenderComponent(
            Actor owner,
            RenderPass renderPass,
            Effect effect,
            VertexPositionColorNormal[] vertices,
            int[] indices,
            IEventManager eventManager)
        {
            this.Owner = owner;

            TransformComponent transformComponent =
                (TransformComponent)this.Owner.GetComponent<TransformComponent>();

            if (transformComponent == null)
            {
                throw new Exception("RenderComponent can only be used with actor's that have transform components");
            }

            eventManager.TriggerEvent(
                new RenderComponentCreatedEvent(
                    this.Owner.Id,
                    new PrimitiveSceneNode(
                        this.Owner.Id,
                        renderPass,
                        transformComponent.Transform,
                        effect,
                        vertices,
                        indices)));
        }

        #endregion

        #region IRenderComponent

        public Actor Owner { get; private set; }

        public void Update(GameTime gameTime)
        {
            // Render components don't update
        }

        public void Dispose()
        {
        }

        #endregion
    }
}
